import http from "@/common/request";

import my, * as mys from './my' 
import user from './user'

let apiArr = [
  ...my,
	...user
];

let apis = {
	...delete mys.default && mys
};
for (let i = 0; i < apiArr.length; i++) {
  let { name } = apiArr[i];
  apis[name] = (data0 = {}, data1 = {}) => {
		let data = {}, opts = {};

		// 通过vuex调用  ( 一个对象参数，如果对象里面有opts，数据取对象下的data，配置取对象下的opts。   如果对象下没有opts, 数据取对象，配置默认 )
		if(data0.dispatch && data0.commit && data0.getters && data0.state) {
			if(data1.opts) {  // 传递了配置
				opts = data1.opts;
				data = data1.data;
			} else {    // 未传递配置
				data = data1;
			}
		} else {  // 普通方式引入调用（两个参数，data, opts）
			data = data0;
			opts = data1;
		}
		let opt = Object.assign({}, apiArr[i], opts);
		try{
			opt.name = opt.name.trim();
			opt.url = opt.url.trim();
		} catch(e){}
		delete opt.name;
    return http.request({
			...opt,
      data,
    });
  };
}
module.exports = apis;
